<?php

namespace app\common\model;

use app\common\Base;
use think\Model;

class Answer extends Model
{
    //引入特性
    use Base;

    // 关联文章表
    public function ansToQue()
    {
        return $this->belongsTo('Question','qid');
    }

    //关联用户表
    public function ansToUser()
    {
        return $this->belongsTo('User','uid');
    }

    //获取某个问题一个的答案
    public function getOneAns($qid)
    {
        $re = $this->where('qid',$qid)->order('a_zan','desc')->limit(1)->find();
        $re['user'] = $re->ansToUser->user;
        $re['userimg'] = $re->ansToUser->pic;
        return $re;
    }

    //获取某个问题全部的答案
    public function getAllAns($qid,$arr)
    {
        $rows = pageque($this,$arr,['qid'=>$qid]);
        //$re = $this->where('qid',$qid)->order('a_zan','desc')->limit(1)->find();
        foreach($rows as $v){
            $v['user'] = $v->ansToUser->user;
            $v['userimg'] = $v->ansToUser->pic;
        }
        return $rows;
    }

    // 获取指定的一个答案
    public function getAns($aid)
    {
        $row = $this->findOrFail($aid);
        $row['user'] = $row->ansToUser->user;
        $row['userimg'] = $row->ansToUser->pic;
        return $row;
    }

    //获取某个用户的答案
    public function getUserAns($uid,$arr)
    {
        $rows = pageque($this,$arr,['uid'=>$uid]);
        foreach($rows as $v){
            /*$v['user'] = $v->ansToUser->user;
            $v['userimg'] = $v->ansToUser->pic;*/
            $v['que'] = $v->ansToQue->title;
            $v['con'] = $v->ansToQue->content;
        }
        return $rows;
    }
}
